package com.draem0507.refactor;

/**
 * @title 使用多态代替条件判断
 * @desc
 * @atuh lwx
 * @createtime on 2015/11/14 17:41
 */
public class Day_31 {


    public static void main(String[] args) {


        Day_31 day_31 = new Day_31();


        Parent parent = new Parent();
        Son son = new Son();
        Daughter daughter = new Daughter();


        day_31.invokeSay(parent);
        day_31.invokeSay(son);
        day_31.invokeSay(daughter);

        System.out.println("华丽的分割线");
        //使用动态方式
        day_31.invokeSay2(parent);
        day_31.invokeSay2(son);
        day_31.invokeSay2(daughter);
        //考虑重载解决 -->又涉及到单分派-->通过使用访问者模式来解决


        System.out.println("--------------------------");


        Parent parent1 = new Parent();
        Parent son1 = new Son();
        Parent daughter1 = new Daughter();

        SingleComplite singleComplite=new SingleComplite();
        singleComplite.say(parent1);
        singleComplite.say(son1);
        singleComplite.say(daughter1);


    }


    public void invokeSay(Object parent) {

        if (parent instanceof Son) {

            ((Son) parent).say();
        } else if (parent instanceof Daughter) {

            ((Daughter) parent).say();
        } else {
            ((Parent) parent).say();
        }
    }

    public void invokeSay2(Parent parent) {

        parent.say();
    }


}


class SingleComplite {

    public void say(Parent parent) {

        parent.say();
    }

}

class Parent {

    public void say() {

        System.out.println("parent say");
    }


}

class Son extends Parent {

    public void say() {

        System.out.println("Son say");
    }

}

class Daughter extends Parent {

    public void say() {

        System.out.println("Daughter say");
    }

}
